﻿///<copyright>
/// Copyright © 2009 Stephen Styrchak. All Rights Reserved.
///</copyright>
using System;
using System.ComponentModel;
using Ferpect.ComponentModel.Design;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ferpect.ComponentModel.Sprites
{
    public class ImageListSprite : ImageSprite
    {
        public ImageListSprite()
        {
            this.Rows = 1;
            this.Columns = 1;
        }

        [Category("Sprite Sheet")]
        [DefaultValue(1)]
        public int Rows { get; set; }

        [Category("Sprite Sheet")]
        [DefaultValue(1)]
        public int Columns { get; set; }

        [Category("Sprite Sheet")]
        [DefaultValue(0)]
        public int RowIndex { get; set; }

        [Category("Sprite Sheet")]
        [DefaultValue(0)]
        public int ColumnIndex { get; set; }

        protected Rectangle? SourceRectangle
        {
            get
            {
                Rectangle? source = null;

                int width = this.Texture.Width / this.Columns;
                int height = this.Texture.Height / this.Rows;
                source = new Rectangle(this.ColumnIndex * width, this.RowIndex * height, width, height);

                return source;
            }
        }

        public override void Draw(SpriteBatchStack spriteBatchStack)
        {
            if (spriteBatchStack == null) throw new ArgumentNullException("spriteBatchStack");

            if (this.Texture != null && this.Visible)
            {
                spriteBatchStack.Current.Draw(this.Texture, this.Position, this.SourceRectangle, this.Color, this.Rotation, this.Origin, this.Scale, this.Effects, this.LayerDepth);
            }
        }

        public override Vector2 GetSize()
        {
            Rectangle? source = this.SourceRectangle;
            if (source != null)
            {
                return new Vector2(source.Value.Width, source.Value.Height);
            }
            else if (this.Texture != null)
            {
                return new Vector2(this.Texture.Width, this.Texture.Height);
            }
            return Vector2.Zero;
        }
    }
}
